package com.cjm.connect;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

//c3p0连接池
public class test10 {
    public static void main(String[] args) throws PropertyVetoException, SQLException, IOException {
        c3po();
    }
    public static void c3po() throws IOException, PropertyVetoException, SQLException {
//        创建一个数据源对象
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//        通过配置文件propertises获取数据连接
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/connect"));
//        获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
//        给数据源comboPooledDateSource赋值
        comboPooledDataSource.setJdbcUrl(url);
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);
        comboPooledDataSource.setDriverClass(driver);
//        设置初始化连接数
        comboPooledDataSource.setInitialPoolSize(10);
//       设置最大连接数
        comboPooledDataSource.setMaxPoolSize(50);
//        连接数据库
        Connection connection = comboPooledDataSource.getConnection();
        System.out.println("连接成功");
//        关闭连接
        connection.close();
    }
}
